setwd("~/lab2r")                                                                         #set your working directory

data.wrist <- read.table("outWrist.txt", sep="\t", header=FALSE, colClasses=rep("numeric", 7))          #read the signals from the wrist sensor
data.leg <- read.table("outLeg.txt", sep="\t", header=FALSE, colClasses=rep("numeric", 7))              #read the signals from the leg sensor
data.torso <- read.table("outTorso.txt", sep="\t", header=FALSE, colClasses=rep("numeric", 7))          #read the signals from the torso sensor
data.labels <- read.table("labelsCode.txt", sep="\t", header=FALSE, colClasses=rep("numeric", 2))       #read the labels

data.names <- c("wrist", "leg", "torso", "label")                                                       #create a vector with the names of the datasets
data.starts <- c(min(data.wrist[,1]), min(data.leg[,1]), min(data.torso[,1]), min(data.labels[,1]))     #create a vector with the starting times (first column is the time stamp)
data.ends <- c(max(data.wrist[,1]), max(data.leg[,1]), max(data.torso[,1]), max(data.labels[,1]))       #create a vector with the ending times (first column is the time stamp)

data.names[order(data.starts, decreasing=FALSE)]                                                        #order the names according to the starting time (find who starts last)
max(data.starts)                                                                                        #print the starting time for the dataset starting the last

data.names[order(data.ends, decreasing=FALSE)]                                                          #order the names according to the ending time (find who ends first)
min(data.ends)                                                                                          #print the ending time for the dataset ending the first


time.stamp.min <- max(data.starts)                                                                        #-------- complete --------
time.stamp.max <- min(data.ends)                                                                      #-------- complete --------


to.keep.wrist <- (data.wrist[,1] >= time.stamp.min) & (data.wrist[,1] < time.stamp.max)                 #create a boolean vector with the rows that mus be kept
data.wrist.clipped <- data.wrist[to.keep.wrist,]                                                        #and extract these rows

to.keep.leg <- (data.leg[,1] >= time.stamp.min) & (data.leg[,1] < time.stamp.max)                       #create a boolean vector with the rows that mus be kept
data.leg.clipped <- data.leg[to.keep.leg,]                                                              #and extract these rows

to.keep.torso <- (data.torso[,1] >= time.stamp.min) & (data.torso[,1] < time.stamp.max)                 #create a boolean vector with the rows that mus be kept
data.torso.clipped <- data.torso[to.keep.torso,]                                                        #and extract these rows

to.keep.labels <- (data.labels[,1] >= time.stamp.min) & (data.labels[,1] < time.stamp.max)              #create a boolean vector with the rows that mus be kept
data.labels.clipped <- data.labels[to.keep.labels,]                                                     #and extract these rows

write.table(data.wrist.clipped[,-1], "wristClipped.txt", sep="\t", row.names=FALSE, col.names=FALSE)    #save the resulting dataset in a text file
write.table(data.leg.clipped[,-1], "legClipped.txt", sep="\t", row.names=FALSE, col.names=FALSE)        #save the resulting dataset in a text file
write.table(data.torso.clipped[,-1], "torsoClipped.txt", sep="\t", row.names=FALSE, col.names=FALSE)    #save the resulting dataset in a text file
write.table(data.labels.clipped[,-1], "labelsClipped.txt", sep="\t", row.names=FALSE, col.names=FALSE)  #save the resulting dataset in a text file
